var orgId = -1;
var myChart = echarts.init(document.getElementById('main'));
var option;
$(function(){


    /**初始化站点**/
    initOrigin();
    /**检测是否选择站点**/
    checkStation();
    loadReport('day-one');

    //日期切换
    $('.payformid-title li').on('click', function () {
        $(this).addClass('active').siblings().removeClass('active');
        var today = $(this).attr('data-id');
        loadReport(today);
    });

    /**底部菜单**/
    $('.weui-tabbar__item').on('click', function () {
        var url = $(this).attr('data-url');
        $(this).siblings().removeClass("weui-bar__item_on-yes");
        $(this).find(".icon-confirm").addClass("weui-bar__item_on-yes");
        if (!isNull(url))
            window.location.href = url;
    });

    /**点击站点**/
    $('.toolbar-station').on('click', function () {
       $('.TipsWindown').show();
    });

    /**点击站点选项**/
    $('.station-list').on('click', 'dt', function () {
        $(this).siblings().find(".icon-confirm").removeClass("confirm-yes");
        $(this).find(".icon-confirm").addClass("confirm-yes");
        orgId = $(this).find(".icon-confirm").attr('data-id');
    });

    /**选择站点**/
    $('.confirm-station').on('click', function () {
        var oid = $('.toolbar-station').attr('data-id');
        if (isNull(oid) || oid == -1) {
            wapAlert("请选择站点！");
            return;
        }
        wapLoading.show();
        $.ajax({
            type: "POST",
            url: "/console/chose/station",
            async: true,
            dataType: 'json',
            timeout: 30000,
            data:{ originId : orgId},
            success: function(res) {
                wapLoading.hide();
                if(res.code == 0) {
                    $('.toolbar-station').attr('data-id', res.data.originId);
                    $('.toolbar-station').html('站点(' + res.data.originName + ')');
                    $('.TipsWindown').hide();
                    initMenu();
                } else {
                    wapAlert(res.msg);
                    return;
                }

            },
            error: function(res) {
                wapAlert('系统异常,请稍后再试');
                wapLoading.hide();
                return;
            },
            complete: function(XMLHttpRequest, status) {
                if(status == 'timeout') {
                    wapAlert('网络链接超时,请稍后再试');
                    wapLoading.hide();
                    return;
                }
            }
        });
    });

    /**初始化菜单**/
    function initMenu() {
        wapLoading.show();
        $.ajax({
            type: "POST",
            url: "/cloud/load/menu",
            async: true,
            dataType: 'json',
            timeout: 30000,
            success: function(res) {
                wapLoading.hide();
                if(res.code == 0) {
                    //具体数据列表
                    var datas = res.data;
                    var mList = '';
                    // console.log(datas)
                    $.each(datas, function(index, val) {
                        mList += "<div data-url='" + val.url + "' data-id ='" + val.id + "' class='weui-cells__title menu-parent'>"
                            + val.name + "</div>";
                        mList += '<div class="weui-grids menu-grid">';
                        if(val.children != undefined && val.children != null && val.children.length > 0) {
                            $.each(val.children, function(i, v) {
                                var icon = v.icon == null ? '/static/console/img/icon_nav_msg.png' : v.icon;
                                mList += '<a href="javascript:;" data-url="' + v.url + '" class="weui-grid js_grid menu-button">' +
                                    '<div class="weui-grid__icon">' +
                                    '<img src="' + icon + '" alt=""></div>' +
                                    '<p class="weui-grid__label">' + v.name + '</p></a>';
                            });
                        }
                        mList += "</div>";
                    });
                    $(".weui-panel_access").empty().append(mList);
                } else {
                    wapAlert(res.msg);
                    wapLoading.hide();
                    return;
                }

            },
            error: function(res) {
                wapAlert(res.msg);
                wapLoading.hide();
                return;
            },
            complete: function(XMLHttpRequest, status) {
                if(status == 'timeout') {
                    wapAlert("请求超时");
                    wapLoading.hide();
                    return;
                }
            }
        });
    }

    /**加载图表**/
    function loadReport(val) {
        if (val == 'day-one') {
            option = {
                title: {
                    text: '驰旭混凝土生产量',
                    subtext: '今日生产计划动态',
                    left: 'center'
                },
                tooltip: {
                    trigger: 'item'
                },
                // legend: {
                //     orient: 'vertical',
                //     left: 'top',
                // },
                series: [
                    {
                        name: '方量',
                        type: 'pie',
                        radius: '50%',
                        data: [
                            {value: 5048, name: '计划[5048(方)]'},
                            {value: 7335, name: '生产[7335(方)]'},
                            {value: 580, name: '发货[580(方)]'},
                            {value: 484, name: '退转[484(方)]'},
                            {value: 300, name: '签单[300(方)]'}
                        ],
                        emphasis: {
                            itemStyle: {
                                shadowBlur: 10,
                                shadowOffsetX: 0,
                                shadowColor: 'rgba(0, 0, 0, 0.5)'
                            }
                        }
                    }
                ]
            };
        } else if (val == 'day-two') {
            option = {
                title: {
                    text: '驰旭混凝土生产量',
                    subtext: '昨天生产计划动态',
                    left: 'center'
                },
                tooltip: {
                    trigger: 'item'
                },
                // legend: {
                //     orient: 'vertical',
                //     left: 'top',
                // },
                series: [
                    {
                        name: '方量',
                        type: 'pie',
                        radius: '50%',
                        data: [
                            {value: 3033, name: '计划[3033(方)]'},
                            {value: 3345, name: '生产[3345(方)]'},
                            {value: 3640, name: '发货[3640(方)]'},
                            {value: 234, name: '退转[234(方)]'},
                            {value: 420, name: '签单[420(方)]'}
                        ],
                        emphasis: {
                            itemStyle: {
                                shadowBlur: 10,
                                shadowOffsetX: 0,
                                shadowColor: 'rgba(0, 0, 0, 0.5)'
                            }
                        }
                    }
                ]
            };
        } else if (val == 'day-three') {
            option = {
                title: {
                    text: '驰旭混凝土生产量',
                    subtext: '今日生产计划动态',
                    left: 'center'
                },
                tooltip: {
                    trigger: 'item'
                },
                // legend: {
                //     orient: 'vertical',
                //     left: 'top',
                // },
                series: [
                    {
                        name: '方量',
                        type: 'pie',
                        radius: '50%',
                        data: [
                            {value: 8234, name: '计划[8234(方)]'},
                            {value: 8347, name: '生产[8347(方)]'},
                            {value: 7353, name: '发货[7353(方)]'},
                            {value: 452, name: '退转[452(方)]'},
                            {value: 463, name: '签单[463(方)]'}
                        ],
                        emphasis: {
                            itemStyle: {
                                shadowBlur: 10,
                                shadowOffsetX: 0,
                                shadowColor: 'rgba(0, 0, 0, 0.5)'
                            }
                        }
                    }
                ]
            };
        }
        myChart.setOption(option);
    }

    /**检查站点**/Volume
    function checkStation() {
        var originId = $('.toolbar-station').attr('data-id');
        if (!isNull(originId))
            return ;

        wapLoading.show();
        $.ajax({
            type: "POST",
            url: "/console/check/station",
            async: true,
            dataType: 'json',
            timeout: 30000,
            success: function(res) {
                wapLoading.hide();
                if(res.code == 0) {
                    $('.toolbar-station').attr('data-id', res.data.originId);
                    $('.toolbar-station').html('站点(' + res.data.originName + ')');
                    initMenu();
                } else {
                    $('.TipsWindown').show();
                    return;
                }

            },
            error: function(res) {
                wapAlert(res.msg);
                wapLoading.hide();
                return;
            },
            complete: function(XMLHttpRequest, status) {
                if(status == 'timeout') {
                    wapAlert('网络链接超时,请稍后再试');
                    wapLoading.hide();
                    return;
                }
            }
        });
    }

    /**初始化站点**/
    function initOrigin() {
        $.ajax({
            type: "post",
            url: '/sys_origin/query/user/origin',
            async: true,
            dataType: 'json',
            timeout: 30000,
            data: {type:2,status:0},
            success: function(res) {
                if(res.code == 0) {
                    var html ='';
                    $.each(res.data, function(index, val) {
                        if (index == 0)
                            html +='<dt><span>' + val.originName + '</span> <i data-id="' + val.id +
                                '" class="wnsM icon-confirm confirm-yes"></i></dt>';
                        else
                            html +='<dt><span>' + val.originName + '</span> <i data-id="' + val.id +
                                '" class="wnsM icon-confirm"></i></dt>';
                    });
                    $('.station-list').empty().append(html);
                }
            },
            error: function(res) {
                wapAlert(res.msg);
                return;
            },
            complete: function(XMLHttpRequest, status) {
                if(status == 'timeout') {
                    wapAlert('网络链接超时,请稍后再试');
                    return;
                }
            }
        });
    }
});
